package com.youdu.netwrok.http;


import android.content.res.AssetManager;
import android.util.Log;

import com.youdu.application.ImoocApplication;
import com.youdu.vuandroidadsdk.adutil.ResponseEntityToModule;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Title:RequestJsonCenter
 * @Package:com.youdu.netwrok.http
 * @Description:
 * @Auther:YJH
 * @Email:yuannunhua@gmail.com
 * @Date:2018/7/2416:56
 */
public class RequestJsonCenter {

    public static Object getJson(String fileName, Class<?> mClass) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = ImoocApplication.getInstance().getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("error", "---------------->>" + e.toString());
        }


        String result = stringBuilder.toString();
        try {
            JSONObject jsonData = new JSONObject(result);
            return ResponseEntityToModule.parseJsonObjectToModule(jsonData, mClass);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }


}
